//
//  PlayDetailView.m
//  Slice
//
//  Created by lanou on 16/6/20.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "PlayDetailView.h"
#define locaFolderPath [NSHomeDirectory() stringByAppendingString:@"/Documents/download"]
@implementation PlayDetailView


- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

//初始化方法,传入所有歌曲
- (instancetype)initWithFrame:(CGRect)frame musicList:(NSMutableArray *)musicList {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = musicList;
        
        self.rootScrollView = [[UIScrollView alloc] initWithFrame:frame];
        
        self.rootScrollView.contentSize = CGSizeMake(frame.size.width * 3, 0);
        
        self.rootScrollView.pagingEnabled = YES;
        
        [self addSubview:self.rootScrollView];
        
//        初始化tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 64) style:UITableViewStylePlain];
        
        [self.rootScrollView addSubview:self.tableView];
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.rootScrollView.contentSize.width / 2  - self.frame.size.width * 0.8 / 2 , 20, frame.size.width * 0.8, frame.size.width * 0.8)];
        
        self.imageView.backgroundColor = [UIColor redColor];
        
        [self.rootScrollView addSubview:self.imageView];
        
        self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width, self.imageView.frame.size.height + self.imageView.frame.origin.y + 20, frame.size.width, 30)];
        
        self.titleLable.backgroundColor = [UIColor grayColor];
        
        [self.rootScrollView addSubview:self.titleLable];
        
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(frame.size.width* 2,0, frame.size.width, frame.size.height)];
        
        [self.rootScrollView addSubview:self.webView];
        
        self.tableView.dataSource = self;
        
        self.tableView.delegate = self;
        
        self.rootScrollView.delegate = self;
        
        [self.tableView registerNib:[UINib nibWithNibName:@"PlayerDetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
    }
    return self;
}

#pragma mark -代理方法-
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    判断是那个
    if (scrollView != self.rootScrollView) {
        return;
    }
    
    if (self.sendValue) {
        self.sendValue(scrollView.contentOffset.x);
    }
   }


#pragma mark -代理方法-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PlayerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    RadioDetailModel *model = self.dataSource[indexPath.row];
    
    [cell getDataByModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if ([model.tingid isEqualToString:_currentModel.tingid]) {
        cell.isPlayingLable.backgroundColor = [UIColor blueColor];
    } else {
        cell.isPlayingLable.backgroundColor = [UIColor redColor];
    }
    
//    添加下载按钮的动作方法
    [cell.loadBtn addTarget:self action:@selector(downLoader:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.loadBtn.tag = indexPath.row  + 500;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"'%@",self.tableView.indexPathForSelectedRow);
    
    
    
    if (self.indexPath != indexPath) {
        PlayerDetailTableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
        cell.isPlayingLable.backgroundColor = [UIColor redColor];
    }
    self.indexPath = indexPath;
    
    PlayerDetailTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    cell.loadBtn.tag = indexPath.row + 1000;
    
    cell.isPlayingLable.backgroundColor = [UIColor blueColor];
}


- (void)setCurrentModel:(RadioDetailModel *)currentModel {
    _currentModel  = currentModel;
   [self.imageView sd_setImageWithURL:[NSURL URLWithString:currentModel.coverimg] placeholderImage:[UIImage imageNamed:@"1.jpeg"]];

    self.titleLable.text = currentModel.title;
    
//
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:currentModel.playInfo[@"webview_url"]]];
    
    [self.webView loadRequest:request];

    [self.tableView reloadData];
}

//下载按钮的方法
- (void) downLoader:(UIButton *)loadBtn  {
//    取出对应的model
      RadioDetailModel *model = self.dataSource[loadBtn.tag - 500];
//
//    DownLoad *loader = [[DownLoad alloc] initWithURL:model.musicUrl saveNameL:model.title];
//    [loader beginLoad];
 DownLoad *loader = [[DownLoadMaqnager shareInstace] addDownLoadWithURL:model.musicUrl saveFileName:model.title addResult:^(addResult addResult) {
        switch (addResult) {
            case addResultSuccess: {
                NSLog(@"添加成功");
                [self popMessageWithTitle:@"添加成功" postion:TOP];
                break;
            }
            case addResultLoadFinish:{
                NSLog(@"下载完成");
                [self popMessageWithTitle:@"下载完成" postion:TOP];

                break;
            }
            default:{
                NSLog(@"下载中");
                [self popMessageWithTitle:@"下载中" postion:TOP];
                break;
            }
        }
    }];
    [loader beginLoad];

}


@end
